/**
 * Copyright (c) 2010 David Hamrick, Guy Kopsombut, Emily Ross 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "PassengerVC.h"
#import "ServerComm.h"

@implementation PassengerVC
@synthesize passengerList;

enum PassengerTables {
	PassengerAssignedTable,
	PassengerUnassignedTable
};


-(void)awakeFromNib
{
	_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reload) userInfo:nil repeats:YES];
	[_timer fire];
	
	assignedPassengers = [[NSMutableArray alloc] init];
	unassignedPassengers = [[NSMutableArray alloc] init];

	_assignedTableView.tag = PassengerAssignedTable;
	_unassignedTableview.tag = PassengerUnassignedTable;
	
	
	passengerEdit = [[PassengerEditVC alloc] initWithWindowNibName:@"PassengerEditVC"];
	
	[_unassignedTableview setTarget:self];
	[_unassignedTableview setDoubleAction:@selector(tappedUnassignedRow:)];
	[_unassignedTableview registerForDraggedTypes:[NSArray arrayWithObject:@"PassengerObject"]];
	
	[_assignedTableView setTarget:self];
	[_assignedTableView setDoubleAction:@selector(tappedAssignedRow:)];
}


- (void)tappedUnassignedRow:(id)sender
{
	NSInteger index = [_unassignedTableview selectedRow];
	if(index == -1)
		return;
	
	PassengerEditVC* editVC = [[PassengerEditVC alloc] initWithWindowNibName:@"PassengerEditVC"];
	editVC.passenger = [unassignedPassengers objectAtIndex:index];
	[editVC showWindow:sender];
}

- (void)tappedAssignedRow:(id)sender
{
	NSInteger index = [_assignedTableView selectedRow];
	if(index == -1)
		return;
	
	PassengerEditVC* editVC = [[PassengerEditVC alloc] initWithWindowNibName:@"PassengerEditVC"];
	editVC.passenger = [assignedPassengers objectAtIndex:index];
	[editVC showWindow:sender];	
}

- (void)reload
{
	[assignedPassengers removeAllObjects];
	[unassignedPassengers removeAllObjects];
	self.passengerList = [Passenger allPassengers];
	for(Passenger* p in passengerList)
	{
		if([[p status] isEqualToString:@"null"])
			[unassignedPassengers addObject:p];
		else 
			[assignedPassengers addObject:p];
	}
	[_unassignedTableview reloadData];
	[_assignedTableView reloadData];
	

}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
	switch([aTableView tag])
	{
		case PassengerAssignedTable:
			return [assignedPassengers count];
		case PassengerUnassignedTable:
			return [unassignedPassengers count];
		default:
			return 0;
	}
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
	switch([aTableView tag])
	{
		case PassengerAssignedTable:
			return [[assignedPassengers objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]];
		case PassengerUnassignedTable:
			return [[unassignedPassengers objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]];
		default:
			return 0;
	}
}
	 
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
	NSInteger passengerID = [[unassignedPassengers objectAtIndex:[rowIndexes firstIndex]] ID];
	[pboard declareTypes:[NSArray arrayWithObject:@"PassengerObject"] owner:self];
	[pboard setData:[NSData dataWithBytes:&passengerID length:sizeof(NSInteger)] forType:@"PassengerObject"];
	return YES;
}


- (IBAction)buttonPressed:(id)sender
{
	if(![self fieldsComplete])
		return;
	
	Passenger* passenger = [[Passenger alloc] init];
	[passenger setName:[_nameField stringValue]];
	[passenger setAddress:[_addressField stringValue]];
	[passenger setPhone:[_phoneField stringValue]];
	[passenger save];

	[passenger release];

	[self clearFields];
	[_unassignedTableview reloadData];
	[_assignedTableView reloadData];
}
- (BOOL)fieldsComplete
{
	NSString* alertText = @"";
	
	if([[_nameField stringValue] length] <= 0 ||
		[[_phoneField stringValue] length] <= 0 ||
		[[_addressField stringValue] length] <= 0 )
		alertText = @"Must complete all fields";
	else if([[_nameField stringValue] length] >= 30 ||
			  [[_phoneField stringValue] length] >= 30 ||
			  [[_phoneField stringValue] length] >= 30)
	{
		alertText = @"Field to long";
	}
	else 
	{
		return YES;
	}


	NSAlert* alert = [[NSAlert alloc] init];
	[alert addButtonWithTitle:@"Ok"];
	[alert setMessageText:alertText];
	[alert beginSheetModalForWindow:window
							modalDelegate:self
						  didEndSelector:@selector(blankSelector)
							  contextInfo:nil];
	return NO;
}

- (void)clearFields
{
	[_nameField setStringValue:@""];
	[_phoneField setStringValue:@""];
	[_addressField setStringValue:@""];
}

@end
